package cn.jbolt.admin.msgcenter;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.JBoltApiRet;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.model.SysNotice;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.para.JBoltNoUrlPara;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltSysNoticeService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;

@CheckPermission(PermissionKey.SYS_NOTICE)
@UnCheckIfSystemAdmin
@Path("/api/admin/sysNotice")
public class SysNoticeAdminController extends JBoltApiBaseController {
    @Inject
    private JBoltSysNoticeService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 单个详情
     */
    @JBoltHttpGet
    public void index() {
        JBoltApiRet ret = findModelById();
        SysNotice notice = ret.getData();
        if (notice != null) {
            service.processReceiverValues(notice);
        }
        renderJBoltApiRet(ret);
    }

    /**
     * 分页条件查询数据源
     * @param page
     * @param pageSize
     * @param type
     * @param priorityLevel
     * @param reciverType
     * @param isDeleted
     * @param keywords
     * @param sortColumn
     * @param sortType
     */
    @JBoltHttpGet
    public void datas(@Para(value = "page",defaultValue = "1")Integer page,
                      @Para(value = "pageSize",defaultValue = "15")Integer pageSize,
                      @Para("type")Integer type,
                      @Para("prioritylevel")Integer priorityLevel,
                      @Para("reciverType")Integer reciverType,
                      @Para(value = "isDeleted",defaultValue = "false")Boolean isDeleted,
                      @Para("keywords")String keywords,
                      @Para(value = "sortColumn",defaultValue = "id")String sortColumn,
                      @Para(value = "sortType",defaultValue = "desc")String sortType
                      ) {
        renderJBoltApiSuccessWithData(service.paginateAdminDatas(page,pageSize,type,priorityLevel,reciverType,keywords,isDeleted,sortColumn,sortType));
    }



    /**
     * 保存
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("") SysNotice sysNotice) {
        renderJBoltApiRet(service.save(sysNotice));
    }

    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")SysNotice sysNotice) {
        renderJBoltApiRet(service.update(sysNotice));
    }

    /**
     * 删除
     */
    @Before({Tx.class,JBoltNoUrlPara.class})
    @JBoltHttpGet
    public void delete() {
        renderJBoltApiRet(service.deleteNotices(getIds(),false));
    }

    /**
     * 批量还原恢复回收站数据
     */
    @Before(Tx.class)
    public void restore() {
        renderJson(service.restoreNotices(getIds()));
    }
}
